<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        return $this->view->fetch();
    }

    public function news()
    {
        $newslist = [];
        return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
    }

    public function validate5()
    {
        $str = '
order_id,
seller_id,
sellerproduct_id,
num,
totalprice,
seller_id,
price,
productname,
image
';

        $array = explode(',', $str);
        echo "protected \$rule = [<br/>";
        foreach ($array as $item) {
            $item = trim($item);
            echo "'$item' => 'require',<br/>";
        }
        echo "];<br/>";


    }

    public function id5($id = 0)
    {
        $str = '
user_id,
seller_id,
sellerproduct_id
';
        //转换成数组，循环输出 $'a' = $this->request->request('a');
        $array = explode(',', $str);
        /**
         * if (!\app\api\model\___::get($seller_id)) {
                $this->error('没有这个商家');
            }
         * $id = $this->request->request('id');
            if(!$id){
                $this->error(__('Invalid parameters'));
            }
         */
        if($id != 0){
            echo "\$id = \$this->request->request('id');<br/>";
            echo "if(!\$id){<br/>";
            echo "\$this->error(__('Invalid parameters'));<br/>";
            echo "}<br/>";
        }
        foreach ($array as $item) {
            $item = trim($item);
            echo "$$item = $" . "this->request->request('$item');<br/>";
            echo "if (!\app\api\model\___::get(\$$item)) {<br/>";
            echo "\$this->error('没有这个__');<br/>";
            echo "}<br/>";
        }


    }




    /**
     * @param $model
     */

    public function save5($model,$id = 0)
    {
        if($id != 0){
            echo "if (!\app\api\model\\$model::get(\$id)) {<br/>";
            echo "\$this->error('没有这个__');<br/>";
            echo "}<br/>";
        }
        echo "\$model = new \app\api\model\\"."$model();<br/>";
        if($id == 0){
            echo "\$result = \$model->allowField(true)->validate(true)->save(\$_POST);<br/>";
        }else{
            echo "\$result = \$model->allowField(true)->validate(true)->save(\$_POST,['id' => \$id]);<br/>";
        }

        echo "if (\$result) {<br/>";
        echo "\$this->success('成功');<br/>";
        echo "}<br/>";
        echo "\$this->error(\$model->getError());<br/>";
    }

    public function delete5($model)
    {
        echo "\$id = \$this->request->request('id');<br/>";
        echo "if(!\$id){<br/>";
        echo "\$this->error(__('Invalid parameters'));<br/>";
        echo "}<br/>";

        echo "\$model = \app\api\model\\$model::get(\$id);<br/>";

        echo "if (!\$model) {<br/>";
        echo "\$this->error('找不到这条数据');<br/>";
        echo "}<br/>";

        echo "\$result =\$model->delete();<br/>";
        echo "if (\$result) {<br/>";
        echo "\$this->success('成功');<br/>";
        echo "}<br/>";
        echo "\$this->error(\$model->getError());<br/>";

    }

    public function tab(){

        $str = 'cook,headline,food,message,me';
        echo "\"pages\":[<br/>";
        $array = explode(',',$str);
        foreach ($array as $item){
            echo "\"pages/$item/$item\",<br/>";
        }
        echo "],<br/>";
        $str1 = '学做菜,头条,美食图,消息,我的';
        $array1 = explode(',',$str1);

        echo "\"tabBar\": {<br/>";
        echo "\"backgroundColor\": \"#ffffff\",<br/>";
        echo "\"color\": \"#999999\",<br/>";
        echo "\"selectedColor\": \"#cc1004\",<br/>";
        echo "\"borderStyle\": \"black\",<br/>";
        echo "\"list\": [<br/>";
        for ($x=0;$x<count($array);$x++){
            echo "{";
            echo "\"pagePath\": \"pages/$array[$x]/$array[$x]\",<br/>";
            echo "\"text\": \"$array1[$x]\",<br/>";
            echo "\"iconPath\": \"pages/images/tab/$array[$x]-0.jpg\",<br/>";
            echo "\"selectedIconPath\": \"pages/images/tab/$array[$x]-1.jpg\"<br/>";
            echo "},<br/>";
        }
        echo "]},<br/>";
    }
}
